package cn.bigfire.wzdj.util;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.swing.ScreenUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ZipUtil;
import com.github.romankh3.image.comparison.ImageComparison;
import com.github.romankh3.image.comparison.ImageComparisonUtil;
import com.github.romankh3.image.comparison.model.ImageComparisonResult;
import lombok.SneakyThrows;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ImgCmp {

    @Test
    @SneakyThrows
    public void test1() {
        BufferedImage img1 = ScreenUtil.captureScreen();
        System.out.println("截取第1张图");
        ThreadUtil.sleep(2000);
        BufferedImage img2 = ScreenUtil.captureScreen();
        System.out.println("截取第2张图");
        long start = System.currentTimeMillis();
        ImageComparisonResult result = new ImageComparison(img1, img2).compareImages();
        long end = System.currentTimeMillis();
        System.out.println("比较耗时:" + (end - start) + "毫秒");
        System.out.println("比较状态"+result.getImageComparisonState());
        System.out.println("不同个数:" + result.getRectangles().size());
        System.out.println("不同百分比:" + result.getDifferencePercent());

        ImageComparisonUtil.saveImage(FileUtil.file("result.jpg"), result.getResult());

    }

    @Test
    @SneakyThrows
    public void test2() {
        BufferedImage img1 = ScreenUtil.captureScreen();
        System.out.println("截取第1张图");
        ThreadUtil.sleep(2000);
        BufferedImage img2 = ScreenUtil.captureScreen();
        System.out.println("截取第2张图");

        int width = img1.getWidth();
        int height = img1.getHeight();

        int[] bitmap1 = img1.getRGB(0, 0, width, height, null, 0, width);
        int[] bitmap2 = img2.getRGB(0, 0, width, height, null, 0, width);
        int[] diffMap = new int[bitmap2.length];
        int chgCount = 0;
        for (int i = 0; i < bitmap1.length; i++) {
            if (bitmap1[i] != bitmap2[i]) {
                diffMap[i] = bitmap2[i];
                chgCount++;
            }else {
                diffMap[i] = 0;
            }
        }
        System.out.println("chgCount:" + chgCount);
        BufferedImage diffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        diffImg.setRGB(0, 0, width, height, diffMap, 0, width);


        byte[] img2bytes = jpgBytes(img2);
        long start1 = System.currentTimeMillis();
        byte[] img2Gzip = ZipUtil.gzip(img2bytes);
        long cha2 = System.currentTimeMillis() - start1;
        System.out.println("allJpgZip耗时:" + cha2);
        System.out.println("img图片:" + size(img2bytes.length) + "zip后:" + size(img2Gzip.length) + ",压缩比:" + img2Gzip.length * 100 / img2bytes.length);

        byte[] diffBytes = jpgBytes(diffImg);
        long start2 = System.currentTimeMillis();
        byte[] diffGzip = ZipUtil.gzip(diffBytes);
        long cha4 = System.currentTimeMillis() - start2;
        System.out.println("diffJpgZip耗时:" + cha4);
        System.out.println("diff图片:" + size(diffBytes.length) + "zip后:" + size(diffGzip.length) + ",压缩比:" + diffGzip.length * 100 / diffBytes.length);

    }

    public static String size(int size){
        return ByteUtil.getSize(size);
    }

    @SneakyThrows
    public static byte[] jpgBytes(BufferedImage img){
        long start = System.currentTimeMillis();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, "jpg", baos);
        System.out.println("jpg转字节耗时:" + (System.currentTimeMillis() - start));
       return baos.toByteArray();
    }

    @SneakyThrows
    public static byte[] pngBytes(BufferedImage img){
        long start = System.currentTimeMillis();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(img, "png", baos);
        System.out.println("png转字节耗时:" + (System.currentTimeMillis() - start));
        return baos.toByteArray();
    }


    @Test
    @SneakyThrows
    public void test3() {
        BufferedImage img = ScreenUtil.captureScreen();

        BufferedImage img1 = resizeImage(img, 800, 600);
//        System.out.println("img1像素点数:"+img1.getWidth() * img1.getHeight());

        BufferedImage img2 = resizeImage(img, 3840, 2160);
//        System.out.println("img2像素点数:"+img2.getWidth() * img2.getHeight());

        BufferedImage img3 = zoomByScale(img, 800, 600);
//        System.out.println("img3像素点数:"+img3.getWidth() * img3.getHeight());

        BufferedImage img4 = zoomByScale(img, 3840, 2160);
//        System.out.println("img4像素点数:"+img4.getWidth() * img4.getHeight());
    }

    public static BufferedImage resizeImage(BufferedImage img, int newWidth, int newHeight) {
        long start = System.currentTimeMillis();
        BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, img.getType());
//        System.out.println("imgType:" + img.getType());
        Graphics graphics = resizedImage.getGraphics();
        Image image = img.getScaledInstance(newWidth, newHeight, BufferedImage.SCALE_DEFAULT);
        graphics.drawImage(image, 0, 0, null);
        long end = System.currentTimeMillis();
        long cha = end - start;
//        System.out.println("resizeImage耗时:"+cha);
        return resizedImage;
    }


    public static BufferedImage zoomByScale(BufferedImage img, int newWidth, int newHeight) throws IOException {
        long start = System.currentTimeMillis();
        BufferedImage resizedImage = new BufferedImage(newWidth, newHeight, img.getType());
        Graphics2D graphics = resizedImage.createGraphics();
        Image image = img.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);
        graphics.drawImage(image, 0, 0, null);
        graphics.dispose();
        long end = System.currentTimeMillis();
        long cha = end - start;
        System.out.println("zoomByScale耗时:"+cha);
        return resizedImage;
    }

}
